package com.atguigu.factory.dAbstractFactory.pizzaStore.order;

import com.atguigu.factory.dAbstractFactory.pizzaStore.pizza.Pizza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @program: design-patterns
 * @author: yangshen
 * @description: 订购披萨
 * @create: 2022-01-23 16:00
 **/
public class OrderPizza {

    AbstractFactory abstractFactory;

    /**
     * 构造器
     */
    public OrderPizza(AbstractFactory abstractFactory) {
        setAbstractFactory(abstractFactory);
    }

    private void setAbstractFactory(AbstractFactory abstractFactory){
        Pizza pizza = null;
        String orderType = "";
        //1.可能本类有其他的方法需要使用AbstractFactory，所以设置为成员变量并在此传递对象
        //2.abstractFactory 可能是北京的工厂子类，也可能是伦敦的工厂子类
        this.abstractFactory = abstractFactory;
        do {
            orderType = getType();
            pizza = abstractFactory.createPizza(orderType);
            if (null != pizza){
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            }else {
                System.out.println("订购失败");
                break;
            }
        }while (true);
    }

    /**
     * 用户输入 - 获取客户希望订购的披萨种类
     * @return 具体的披萨类型
     */
    public String getType(){
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("input pizza 种类：");
        try {
            return reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
